/*
 * Tuple.cpp
 *
 *  Created on: 06/02/2014
 *      Author: Bruno
 */

#include "Tuple.h"
#include <iostream>

using namespace std;

int Tuple::tuplesIntSize = 0;

void Tuple::SetTuplesIntSize(int numAct){
	Tuple::tuplesIntSize = 2 * numAct + 3;
}

int Tuple::GetCostFromTupleArray(int * tupleArray){
	return (tupleArray[0] == -1) ? INT_MAX : tupleArray[2];
}

Tuple::Tuple(int tupleLength){

	this->array = new int[2 * tupleLength + 3];
	this->array[0] = -1;
	this->array[1] = 0;
	this->array[2] = 0;
	this->length = tupleLength;

}

Tuple::Tuple(int arraySize, int * array) {

	this->array = new int[arraySize];
	this->length = (arraySize - 3) / 2;
	memcpy(this->array,array,sizeof(int) * arraySize);

}

Tuple::~Tuple() {

	delete[] this->array;
}

bool Tuple::empty(){
	return (this->array[0] == -1);
}

Tuple* Tuple::copy() {

	Tuple* t = new Tuple(this->length);
	memcpy(t->array,this->array,sizeof(int) * Tuple::tuplesIntSize);
	return t;
}

int* Tuple::getArray() {
	return array;
}

int Tuple::lastAct() {
	return this->array[0];
}

int Tuple::time() {
	return this->array[1];
}

int Tuple::cost() {
	return (this->empty()) ? INT_MAX : this->array[2];
}

int Tuple::times(int act) {
	return this->array[3 + act];
}

int Tuple::modes(int act) {
	return this->array[3 + this->length + act];
}

void Tuple::lastAct(int a) {
	this->array[0] = a;
}

void Tuple::time(int t) {
	this->array[1] = t;
}

void Tuple::cost(int c) {
	this->array[2] = c;
}

void Tuple::times(int act, int t) {
	this->array[3 + act] = t;
}

void Tuple::modes(int act, int m) {
	this->array[3 + this->length + act] = m;
}

int Tuple::getLength() {
	return this->length;
}

bool Tuple::isSolution() {

	return (this->length == this->array[0] + 1);

}

void Tuple::print(){
	if (this->empty()){
		printf("The tuple is empty!!\n");
		return;
	}

	int numActs = this->array[0];
	int * times = &this->array[3];
	int * modes = &this->array[3 + this->length];

	printf("Activity\t\tMode\t\tTime\n--------\t\t----\t\t----\n\n");

	for(int act = 0; act <= numActs; act++){

		printf("    %d    \t\t  %d  \t\t  %d  \n",act,modes[act],times[act]);

	}
}

